package com.yenlien.traceyun.business.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.BoxDao;
import com.yenlien.traceyun.business.dao.SubpackageDao;
import com.yenlien.traceyun.business.entity.Box;
import com.yenlien.traceyun.business.entity.Subpackage;
import com.yenlien.traceyun.business.service.rest.usercenter.CompanyRestService;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BoxService {

    @Autowired
    private BoxDao boxDao;

    @Autowired
    private CompanyRestService companyRestService;

    @Autowired
    private SubpackageDao subpackageDao;

    /**
     * 查询12个月每个月赋码量
     *
     * @param companyId
     * @return
     */
    public Object uniqueId(String companyId) {
        JSONObject jsonObject = (JSONObject) companyRestService.getchild(companyId);
        JSONArray companies = jsonObject.getJSONArray("data");
        List<String> getchild = new ArrayList<>();
        for (int i = 0; i < companies.size(); i++) {
            getchild.add(i, String.valueOf(companies.getJSONObject(i).get("id")));
        }
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        Integer month = ca.get(Calendar.MONTH) + 1;
        Integer year = ca.get(Calendar.YEAR);
        String dateStr = year.toString() + "-" + month.toString() + "-01";
        List<Integer> arr = new ArrayList<>();
        try {
            Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);    //当前月
            List<Box> byProfessionAndCreatetimeBetween1 = boxDao.findByCreatetimeBetweenAndCompanyIdIn(startDate, date, getchild);
            if (!StringUtils.isEmpty(byProfessionAndCreatetimeBetween1)) {
                arr.add(0, byProfessionAndCreatetimeBetween1.size());
            } else {
                arr.add(0, 0);
            }
            Calendar ca1 = Calendar.getInstance();
            ca1.setTime(startDate);
            for (int i = 1; i < 12; i++) {
                Date ca1Time = ca1.getTime();
                ca1.add(Calendar.MONTH, -1);
                List<Box> byProfessionAndCreatetimeBetween = boxDao.findByCreatetimeBetweenAndCompanyIdIn(ca1.getTime(), ca1Time, getchild);
                if (byProfessionAndCreatetimeBetween.size() != 0) {
                    arr.add(i, byProfessionAndCreatetimeBetween1.size());
                } else {
                    arr.add(i, 0);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return arr;
    }

    public Object scan(String companyId) {
        JSONObject jsonObject = (JSONObject) companyRestService.getchild(companyId);
        JSONArray companies = jsonObject.getJSONArray("data");
        List<String> getchild = new ArrayList<>();
        for (int i = 0; i < companies.size(); i++) {
            getchild.add(i, String.valueOf(companies.getJSONObject(i).get("id")));
        }
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < getchild.size(); i++) {
            String s = getchild.get(i);
            List<Box> byCompanyId = boxDao.findByCompanyId(s);
            map.put(s, byCompanyId.size());
        }
        return map;

    }

    public Object scanTop(String companyId) {
        JSONObject jsonObject = (JSONObject) companyRestService.getchild(companyId);
        JSONArray companies = jsonObject.getJSONArray("data");
        List<String> getchild = new ArrayList<>();
        for (int i = 0; i < companies.size(); i++) {
            getchild.add(i, String.valueOf(companies.getJSONObject(i).get("id")));
        }
        List<String> byRelevans = subpackageDao.findByproductName();
        Map<String, Integer> map = new HashMap<>();
        for (String s1 : byRelevans) {
            List<Subpackage> byProductNameAndCompanyIdIn = subpackageDao.findByProductNameAndCompanyIdIn(s1, getchild);
            map.put(s1, byProductNameAndCompanyIdIn.size());
        }
        return map;
    }

    /*种植扫码数量统计*/
    public int fuCode(String companyId) {
        return boxDao.fuCode(companyId);
    }

    public List<String> findBySubId(String id) {
        return boxDao.findBySubId(id);
    }
}
